package cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.*;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskInfo.vo.BizRiskInfoRespVO;
import cn.iocoder.yudao.module.biz.convert.risk.basicManage.BizBasicManageConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.basicManage.BizBasicManageDO;
import cn.iocoder.yudao.module.biz.service.risk.basicManage.BizBasicManageService;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 基础管理")
@RestController
@RequestMapping("/biz/basic-manage")
@Validated
public class BizBasicManageController {

    @Resource
    private BizBasicManageService basicManageService;

    @PostMapping("/create")
    @ApiOperation("创建基础管理")
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:create')")
    public CommonResult<String> createBasicManage(@Valid @RequestBody BizBasicManageCreateReqVO createReqVO) {
        return success(basicManageService.createBasicManage(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新基础管理")
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:update')")
    public CommonResult<Boolean> updateBasicManage(@Valid @RequestBody BizBasicManageUpdateReqVO updateReqVO) {
        basicManageService.updateBasicManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除基础管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:delete')")
    public CommonResult<Boolean> deleteBasicManage(@RequestParam("id") String id) {
        basicManageService.deleteBasicManage(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得基础管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:query')")
    public CommonResult<BizBasicManageRespVO> getBasicManage(@RequestParam("id") String id) {
        BizBasicManageDO basicManage = basicManageService.getBasicManage(id);
        return success(BizBasicManageConvert.INSTANCE.convert(basicManage));
    }

    @GetMapping("/list")
    @ApiOperation("获得基础管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:query')")
    public CommonResult<List<BizBasicManageRespVO>> getBasicManageList(@RequestParam("ids") Collection<String> ids) {
        List<BizBasicManageDO> list = basicManageService.getBasicManageList(ids);
        return success(BizBasicManageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得基础管理分页")
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:query')")
    public PageResult<BizBasicManageRespVO> getBasicManagePage(@Valid BizBasicManagePageReqVO pageVO) {
        PageResult<BizBasicManageRespVO> result=new PageResult<>();
        List<BizBasicManageRespVO> list = basicManageService.getBasicPage(pageVO);
        Integer count= basicManageService.getBasicCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出基础管理 Excel")
    @PreAuthorize("@ss.hasPermission('biz:basic-manage:export')")
    @OperateLog(type = EXPORT)
    public void exportBasicManageExcel(@Valid BizBasicManageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizBasicManageDO> list = basicManageService.getBasicForExecl(exportReqVO);
        // 导出 Excel
        List<BizBasicManageExcelVO> datas = BizBasicManageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "基础管理.xls", "数据", BizBasicManageExcelVO.class, datas);
    }

    @PostMapping("/import")
    @ApiOperation("导入基础管理")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class)})
    public CommonResult<BizBasicManageImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<BizBasicManageExcelVO> list = ExcelUtils.read(file, BizBasicManageExcelVO.class);
        return success(basicManageService.importExcel(list));
    }

    @GetMapping("/get-import-template")
    @ApiOperation("获得导入基础管理模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<BizBasicManageExcelVO> list = Arrays.asList(
                BizBasicManageExcelVO.builder().basicName("基础管理名称").basicContent("基础管理内容").basicArea("基础管理区域").explotUnit("实施单位").build());

        // 输出
        ExcelUtils.write(response, "基础管理导入模板.xls", "基础管理列表", BizBasicManageExcelVO.class, list);
    }
}
